package response

import (
	"strings"
	"time"
	"trace-rule/internal/domain/model"
)

type RuleResponse struct {
	ID            uint      `json:"id"`
	Symbol        string    `json:"symbol"`
	MinOperator   string    `json:"minOperator"`
	MinValue      float64   `json:"minValue"`
	MaxOperator   string    `json:"maxOperator"`
	MaxValue      float64   `json:"maxValue"`
	EmailTemplate string    `json:"emailTemplate"`
	Interval      string    `json:"interval"`
	IsEnabled     bool      `json:"isEnabled"`
	CreatedAt     time.Time `json:"createdAt"`
	UpdatedAt     time.Time `json:"updatedAt"`
	TargetEmails  []string  `json:"targetEmails"`
}

type RuleListResponse struct {
	Total int64          `json:"total"`
	Data  []RuleResponse `json:"data"`
}

// 转换函数
func ToRuleResponse(rule *model.Rule) *RuleResponse {
	return &RuleResponse{
		ID:            rule.ID,
		Symbol:        rule.Symbol,
		MinOperator:   rule.MinOperator,
		MinValue:      rule.MinValue,
		MaxOperator:   rule.MaxOperator,
		MaxValue:      rule.MaxValue,
		EmailTemplate: rule.EmailTemplate,
		Interval:      rule.Interval,
		IsEnabled:     rule.IsEnabled,
		CreatedAt:     rule.CreatedAt,
		UpdatedAt:     rule.UpdatedAt,
		TargetEmails:  strings.Split(rule.TargetEmails, ","),
	}
}

func ToRuleListResponse(rules []model.Rule, total int64) *RuleListResponse {
	responses := make([]RuleResponse, 0)
	for _, rule := range rules {
		responses = append(responses, *ToRuleResponse(&rule))
	}
	return &RuleListResponse{
		Total: total,
		Data:  responses,
	}
}
